package cn.seecoder.ai.serviceImpl;

import cn.seecoder.ai.dao.AnalysisConfigRepository;
import cn.seecoder.ai.dao.AnalysisPipelineRepository;
import cn.seecoder.ai.exception.AIExternalException;
import cn.seecoder.ai.model.po.AnalysisConfig;
import cn.seecoder.ai.model.po.AnalysisPipeline;
import cn.seecoder.ai.model.vo.AnalysisPipelineVO;
import cn.seecoder.ai.model.vo.AnalysisVisualizationVO;
import cn.seecoder.ai.service.AnalysisPipelineService;
import cn.seecoder.ai.service.AnalysisVisualizeService;
import cn.seecoder.ai.service.FileInfoService;
import cn.seecoder.ai.utils.POJOConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author DingXiaoyu
 * @date 2023/4/16 15:12
 */
@Service
public class AnalysisPipelineServiceImpl implements AnalysisPipelineService {

    @Autowired
    AnalysisPipelineRepository analysisPipelineRepository;

    @Autowired
    FileInfoService fileInfoService;

    @Autowired
    AnalysisVisualizeService analysisVisualizeService;

    @Autowired
    AnalysisConfigRepository analysisConfigRepository;

    @Override
    public AnalysisPipelineVO addPipeline(AnalysisPipelineVO analysisPipelineVO) {
        AnalysisPipeline analysisPipeline=toPO(analysisPipelineVO);
        analysisPipelineRepository.save(analysisPipeline);
        return toVO(analysisPipeline);
    }

    @Override
    public Boolean deletePipeline(Integer pipelineId) {
        analysisPipelineRepository.deleteById(pipelineId);
        return true;
    }

    @Override
    public List<AnalysisPipelineVO> getPipelines(Integer userId) {
        List<AnalysisPipeline> analysisPipelines=analysisPipelineRepository.findByUserId(userId);
        return analysisPipelines.stream().map(this::toVO).collect(Collectors.toList());
    }

    @Override
    public AnalysisVisualizationVO usePipeline(Integer pipelineId, Integer fileId) {
        AnalysisPipeline analysisPipeline=analysisPipelineRepository.findById(pipelineId).get();
        if (!fileInfoService.isFit(analysisPipeline.getChosenColumns(),fileId)){
            throw AIExternalException.columnsNotExist();
        }
        AnalysisConfig analysisConfig=new AnalysisConfig();
        analysisConfig.setChosenColumns(new ArrayList<>(analysisPipeline.getChosenColumns()));
        analysisConfig.setConfigName(pipelineId +"id生成");
        analysisConfig.setUserId(-analysisPipeline.getUserId());
        analysisConfig.setFileInfoId(fileId);
        analysisConfig.setCreatedTime(new Date());
        analysisConfigRepository.save(analysisConfig);
        return analysisVisualizeService.visualize(analysisConfig.getId(),analysisConfig.getUserId(),analysisPipeline.getAnalysisFunction());
    }

    @Override
    public AnalysisPipelineVO getPipeline(Integer pipelineId) {
        AnalysisPipeline analysisPipeline=analysisPipelineRepository.findById(pipelineId).get();
        return toVO(analysisPipeline);
    }

    private AnalysisPipeline toPO(AnalysisPipelineVO analysisPipelineVO){
        return (AnalysisPipeline) POJOConverter.convert(analysisPipelineVO);
    }

    private AnalysisPipelineVO toVO(AnalysisPipeline analysisPipeline){
        return (AnalysisPipelineVO)POJOConverter.convert(analysisPipeline);
    }
}
